package mysql;

import xdb.Trace;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

public class MySQL4Social {
    public static int GetTableCount(String tableName) throws Exception {
        Connection conn = null;
        PreparedStatement pst = null;
        int count = 0;
        String sql = "SELECT COUNT(*) FROM " + tableName;

        try {
            long tm = System.currentTimeMillis();
            conn = MySqlMgr.GetInstance().GetReadConn();
            pst = conn.prepareStatement(sql);
            ResultSet ret = pst.executeQuery();
            if (ret.next()) {
                count = ret.getInt(1);
            }

            if (Trace.isDebugEnabled()) {
                long useTime = System.currentTimeMillis() - tm;
                Trace.debug("Execute Table Count Use " + useTime + ", Name:" + tableName);
            }
        } catch (Exception e) {
            Trace.error("Table Name is " + tableName, e);
            throw new Exception();
        } finally {
            if (pst != null) {
                try {
                    pst.close();
                } catch (Exception e) {
                    Trace.error("read pst close", e);
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                    Trace.error("read conn close", e);
                }
            }

        }

        return count;
    }

    public static Map<String, String> GetTableValue(String tableName, int limit, int offset) throws Exception {
        Connection conn = null;
        PreparedStatement pst = null;
        Map<String, String> octStreamSet = new HashMap();
        String sql = "SELECT k,v FROM " + tableName + " LIMIT " + limit + " OFFSET " + offset;

        try {
            long tm = System.currentTimeMillis();
            conn = MySqlMgr.GetInstance().GetReadConn();
            pst = conn.prepareStatement(sql);
            ResultSet ret = pst.executeQuery();

            while (ret.next()) {
                octStreamSet.put(ret.getString(1), ret.getString(2));
            }

            if (Trace.isDebugEnabled()) {
                long useTime = System.currentTimeMillis() - tm;
                Trace.debug("Fetch Table Value Use " + useTime + "ms, " + tableName);
            }
        } catch (Exception e) {
            Trace.error("Table Name is " + tableName, e);
            throw new Exception();
        } finally {
            if (pst != null) {
                try {
                    pst.close();
                } catch (Exception e) {
                    Trace.error("read pst close", e);
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                    Trace.error("read conn close", e);
                }
            }

        }

        return octStreamSet;
    }
}
